<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* UGCWasm (https://iclient.supermap.io/web/libs/ugcwasm/1.0.1/UGCWasmAll.js)
*********************************************************************-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title data-i18n="resources.title_ugcRegionWithRect"></title>
    <style>
      body {
        margin: 0;
        overflow: hidden;
        background: #fff;
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
      }
      #toolbar {
        position: absolute;
        top: 50px;
        right: 10px;
        width: 280px;
        text-align: center;
        z-index: 10000;
        border-radius: 4px;
      }
    </style>
  </head>

  <body>
    <div id="toolbar" class="panel panel-primary">
      <div class="panel-heading">
        <h5 class="panel-title text-center">面与矩形相交检测</h5>
      </div>
      <div class="panel-body content">
        <input type="button" class="btn btn-default" value="执行" onclick="search()" />
      </div>
    </div>
    <div id="map" style="width: 100%; height: 100%"></div>
    <script type="text/javascript" include="bootstrap-css" src="../js/include-web.js"></script>
    <script type="text/javascript" include="ugcwasm" src="../../dist/leaflet/include-leaflet.js"></script>
    <script>
      var map, polygon1, polygon2;
      var host = window.isLocal ? window.server : 'https://iserver.supermap.io';
      var url = host + '/iserver/services/map-world/rest/maps/World';
      L.supermap
        .initMap(url, {
          mapOptions: {
            center: [30, 120],
            zoom: 7
          }
        })
        .then((res) => {
          map = res.map;
          addFeatures();
        });

      function addFeatures() {
        polygon1 = {
          type: 'Feature',
          properties: {},
          geometry: {
            coordinates: [
              [
                [119.267912970807, 29.742649250075488],
                [119.267912970807, 28.929660206686734],
                [120.88700327552647, 28.929660206686734],
                [120.88700327552647, 29.742649250075488],
                [119.267912970807, 29.742649250075488]
              ]
            ],
            type: 'Polygon'
          }
        };
        polygon2 = {
          type: 'Feature',
          properties: {},
          geometry: {
            coordinates: [
              [
                [121.21536332646303, 33.19979516422491],
                [121.21536332646303, 29.664917601179866],
                [123.33517321005525, 29.664917601179866],
                [123.33517321005525, 33.19979516422491],
                [121.21536332646303, 33.19979516422491]
              ]
            ],
            type: 'Polygon'
          }
        };
        var targetPolygon = {
          type: 'Feature',
          properties: {},
          geometry: {
            coordinates: [
              [
                [118, 32],
                [118, 30],
                [122, 30],
                [122, 32],
                [118, 32]
              ]
            ],
            type: 'Polygon'
          }
        };
        L.geoJSON([polygon1, polygon2, targetPolygon], {
          style: function () {
            return {
              weight: 5,
              color: 'blue'
            };
          }
        }).addTo(map);
      }

      function search() {
        let intersectCount;
        var geometryAnalysis = new L.supermap.GeometryAnalysis();
        [polygon1, polygon2].forEach((polygon) => {
          intersectCount = geometryAnalysis.isIntersectRegionWithRect(polygon, 118, 32, 122, 30);
          if (intersectCount) {
            L.geoJSON(polygon, {
              style: function () {
                return {
                  weight: 5,
                  color: 'green'
                };
              }
            }).addTo(map);
          }
        });
      }
    </script>
  </body>
</html>
